/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef FORGETTINGCURVEPLOT_H
#define FORGETTINGCURVEPLOT_H

#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_interval_data.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_symbol.h>

#include "histogram_item.h"

#include <map>
using namespace std;

#include <QtGui/QWidget>
#include <QDialog>
#include "ui_ForgettingCurvePlot.h"

class TestResultCounter
{
public:
	TestResultCounter():
		passSampleCount(0),
		failSampleCount(0),
		isValid(false)
	{}

	int sampleCount(){return passSampleCount + failSampleCount;}
	float passRate(){return float(passSampleCount)/float(sampleCount());}

	int passSampleCount;
	int failSampleCount;
	bool isValid;
};

class ForgettingCurvePlot : public QDialog
{
    Q_OBJECT

public:
    ForgettingCurvePlot(QDialog *parent = 0);
    ~ForgettingCurvePlot();
    int exec();

    void setData(map<int, map<int, TestResultCounter> >& testResultCounterMap, map<int , int>& testBestIntvMap);

public slots:
	void testCountChanged(const QString& text);

private:
    void drawSampleDistribution(int testCount);
    void drawForgettingCurve(int testCount);
    void setStatistics(int testCount);

private:
    Ui::ForgettingCurvePlotClass ui;

    map<int, map<int, int> > mSampleDistributionMap; //mSampleDistributionMap[testCount][Interval] = sample count
    map<int, map<int, int> > mSamplePassMap;
    map<int, map<int, int> > mSampleFailMap;
    map<int, map<int, float> > mRetentionMap; //mRetentionMap[testCount][Interval] = retention (pass test rate)
    map<int , int> mTestBestIntv; //mTestBestIntv[testCount] = best day interval
    QStringList mTestCountList;

    QwtPlot mSampleDistributionPlot;
    HistogramItem* mSampleDistributionHistogram;
	QwtPlot mForgettingCurvePlot;
	QwtPlotCurve mForgettingCurveSampleCrv;
	QwtPlotCurve mForgettingCurveExpCrv;
	bool mAutoReplot;
	int mIntvMin;
	int mIntvMax;
};

#endif // FORGETTINGCURVEPLOT_H
